package com.hotelvip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hotelvip.common.annotation.CacheFind;
import com.hotelvip.common.exception.ServiceException;
import com.hotelvip.common.pojo.Tree;
import com.hotelvip.dao.ItemCatMapper;
import com.hotelvip.entity.ItemCat;
import com.hotelvip.service.ItemCatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzh
 * @date 2020/12/17 14:40
 */
@Slf4j
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;
    /**
     * 查分类数据
     * @param parentId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    @CacheFind(key = "ITEM_CAT_PARENTID",seconds = 3600)
    public List<Tree> findItemCats(Long parentId) {
        //根据父级查询数据
        QueryWrapper<ItemCat> itemCatQueryWrapper = new QueryWrapper<>();
        itemCatQueryWrapper.eq("parent_id", parentId).orderByAsc("sort_order");
        List<ItemCat> itemCatsList = itemCatMapper.selectList(itemCatQueryWrapper);
        //封装到tree中
        ArrayList<Tree> itemCatTreeList = new ArrayList<>();
        for (ItemCat itemCat : itemCatsList) {
            Long id = itemCat.getId();
            String text = itemCat.getName();
            String state= itemCat.getIsParent() ? "closed" : "open";
            Tree tree = new Tree(id, text, state);
            itemCatTreeList.add(tree);
        }

        return itemCatTreeList;
    }
    /**
     * 根据分类id查找分类名称
     * @return
     */
    @CacheFind(key = "ITEM_CAT_ID",seconds = 3600)
    @Override
    @Transactional(readOnly = true)
    public String findItemCatById(Long itemCatId) {
        log.info("验证");
        if (itemCatId<0){
//            throw new ServiceException("非法的商品分类");
        }
        log.info("查询");
        ItemCat itemCat = itemCatMapper.selectById(itemCatId);
        if (itemCat==null){
            throw new ServiceException("商品分类名称不存在");
        }
        return itemCat.getName();
    }

    /**
     * 新增分类信息
     * @param itemCat
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveItemCat(ItemCat itemCat) {
        log.info("验证");
        if (itemCat.getParentId()==null){
            throw new ServiceException("请选择父级菜单或更改菜单为父级菜单");
        }
        QueryWrapper<ItemCat> itemCatQueryWrapper = new QueryWrapper<>();
        itemCatQueryWrapper.eq("name", itemCat.getName());
        if (itemCatMapper.selectCount(itemCatQueryWrapper)!=0){
            throw new ServiceException("该分类已存在");
        }
        log.info("初始化");
        //设置默认值，默认正常可用，新增分类无下级，修改上级菜单判断，排序
        itemCat.setStatus(1).setIsParent(false);
        itemCat.setSortOrder(itemCatMapper.selectCount(new QueryWrapper<ItemCat>()
                .eq("parent_id", itemCat.getParentId())));
        log.info("执行添加，并修改关联");
        itemCatMapper.insert(itemCat);
        if (itemCat.getParentId()!=0){
            UpdateWrapper<ItemCat> itemCatUpdateWrapper = new UpdateWrapper<>();
            itemCatUpdateWrapper
                    .eq("id", itemCat.getParentId());
            itemCatMapper.update(new ItemCat().setIsParent(true),itemCatUpdateWrapper);
        }



    }
}
